clear all
set emptycells drop
set matsize 11000
set maxvar 32670

*Set working directory
if c(username)=="" { //insert username
cd "" // insert root path
}

if c(username)=="jonathancolmer" { 
	cd "/Users/jonathancolmer/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder" 
}

if c(username)=="jmc4qg" { 
	cd "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder" 
}

use "Analysis Data/NIBRS_analysis.dta", clear

eststo i2:reghdfe homicide_pc tMean DD_H prec, absorb(ori_sample_month week dow) cluster(fips) keepsingletons
estimates save "Figures and Tables/Figure_A2/2_part_spline.ster", replace

clear all

set more off

local temperatures 10 20 30

local lhsvar homicide_pc

local avg_t `temperature'

local starttemp -10

local endtemp 30

local omit 11

set obs 41

gen temperature = 0.5

keep temperature

replace temperature = -10+_n-1

gen temperature_l = 0
replace temperature_l = temperature - 0 if temperature < 18
replace temperature_l = 18 if temperature >= 18
gen temperature_m = 0 
replace temperature_m = temperature - 18 if temperature > 18 & temperature <30
replace temperature_m = 30 - 18 if temperature >= 30
gen temperature_h = 0
replace temperature_h = temperature - 30 if temperature > 30 

estimate use "Figures and Tables/Figure_A2/2_part_spline.ster"


gen homicide_pc = .

predictnl yhat1 = temperature*(_b[tMean]) -(`omit'*(_b[tMean])), se(se1) ci(lowerci1 upperci1)
predictnl yhat2 = (temperature*(_b[tMean]) + temperature_m*(_b[tMean]+_b[DD_H]))-(`omit'*(_b[tMean])), se(se2) ci(lowerci2 upperci2)


gen YHAT = yhat1 if temperature < 18
replace YHAT = yhat2 if temperature >= 18

gen LC = lowerci1 if temperature < 18
replace LC = lowerci2 if temperature >= 18 

gen UC = upperci1 if temperature < 18
replace UC = upperci2 if temperature >= 18 

graph tw rarea UC LC temperature if temperature >= `starttemp', fint(20) fcolor(black) alwidth(none) || line YHAT temperature if temperature >= `starttemp', lcolor(black) yline(0) lcolor(black) xscale(range(`starttemp' `endtemp')) ylabel(-0.004(0.002)0.006,labsize(large)) xlabel(`starttemp'(10)`endtemp', labsize(large)) graphregion(fcolor(white)) xtitle("Daily Average Temperature", size(large)) ytitle("Change in the Number of Homicides" "per 100,000 people", size(large)) legend(off)
graph export "Figures and Tables/Figure_A2/Figure_A2.png", replace
